<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:TestFormat.php,2010/05/25 15:56:47 
 * 
 **************************************************************************/
 
 
 
/**
 * @file TestFormat.php
 * @author huqingping
 * @date 2010/05/25 15:56:47
 * @version 1.0 
 * @brief 
 *  
 **/

final class TestFormat extends TestCase
{
	private function getData($if,$of)
	{
		$get = array(
			'key1'=>"1",
			'key3'=>'3',
		);
		$post = array(
			'key2'=>"2",
			'key3'=>'3',
		);
		$put = array(
			'kp'=>'value',
			'k2'=>'value',
			);
		$get['_if'] = $if;
		$get['_of'] = $of;
		return array($get,$post,$put);
	}

	private function getPutFile($put)
	{
		$file = '/tmp/HapN_put';
		file_put_contents($file,$put);
		return $file;
	}

	private function checkJson($ret)
	{
		$json = $ret->json();
		$this->assertTrue(!empty($json));
		$this->assertEquals('hapn.ok',$json['err']);
		$this->assertTrue(is_array($json['data']));
		$this->assertEquals(3, $json['data']['key3']);
	}

	private function checkXml($ret)
	{
		$xml = $ret->body;
		$this->assertEquals('<?xml',substr($xml,0,5));
		echo $xml;
	}


	private function checkMcpack($ret)
	{
		$mcpack = $ret->mcpack();
		$this->assertTrue(!empty($mcpack)); 
		$this->assertEquals('hapn.ok',$mcpack['err']);
		$this->assertTrue(is_array($mcpack['data']));
		$this->assertEquals(3, $mcpack['data']['key3']);
	}

	function testDefault()
	{
		list($get,$post) = $this->getData('default','default');
		$get['_if'] = 'default';
		$post['_of'] = 'default';
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'post'=>$post)
		);
		$this->assertTrue(strncmp($ret->body,'Array',5) === 0);
		$this->assertTrue(strpos($ret->body,'[err]') > 0);
		$this->assertTrue(strpos($ret->body,'[data]') > 0);
		$lines = explode("\n",$ret->body);
		$this->assertEquals(12,count($lines));
	}

	function testDefault2Xml()
	{
		list($get,$post) = $this->getData('default','xml');
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'post'=>$post)
		);
		$this->checkXml($ret);
	}

	function testDefault2Json()
	{
		list($get,$post) = $this->getData('default','json');
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'post'=>$post)
		);
		$this->checkJson($ret);
	}

	function testJson2Json()
	{
		list($get,,$put) = $this->getData('json','json');
		$putFile = $this->getPutFile(json_encode($put));
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'put'=>$putFile)
		);
		$this->checkJson($ret);
	}

	function testErr2Json()
	{
		list($get,$post) = $this->getData('default','json');
		$ret = $this->fetch('/HapN/testErr2Json',array('get'=>$get));
		$json = $ret->json();
		$this->assertTrue(!empty($json));
		$this->assertEquals('hapn.err2json',$json['err']);
	}

	function testDefault2Mcpack()
	{
		list($get,$post) = $this->getData('default','mcpack');
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'post'=>$post)
		);
		$this->checkMcpack($ret);
	}

	function testMcpack2Mcpack()
	{
		list($get,$put) = $this->getData('mcpack','mcpack');
		$putFile = $this->getPutFile(mc_pack_array2pack($put));
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'put'=>$putFile)
		);
		$this->checkMcpack($ret);
	}

	function testXBAIDURPC()
	{
		list($get,$put) = $this->getData('mcpack','mcpack');
		$putFile = $this->getPutFile(mc_pack_array2pack($put));
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'put'=>$putFile,'header'=>array('X-BAIDU-RPC: ok'))
		);
		$this->checkMcpack($ret);
	}

	function testJson2Mcpack()
	{
		list($get,$put) = $this->getData('json','mcpack');
		$putFile = $this->getPutFile(json_encode($put));
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'put'=>$putFile)
		);
		$this->checkMcpack($ret);
	}

	function testMcpack2Json()
	{
		list($get,$put) = $this->getData('mcpack','json');
		$putFile = $this->getPutFile(mc_pack_array2pack($put));
		$ret = $this->fetch('/HapN/testFormat',
			array('get'=>$get,'put'=>$putFile)
		);
		$this->checkJson($ret);
	}
}






/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
